package online.flowerinsnow.minecraft.bukkit.manhunt.listener;

import online.flowerinsnow.minecraft.bukkit.manhunt.config.ManhuntConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.Zombie;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class SpecialMonsterListener implements Listener {
    public static final HashMap<UUID, SpecialMonster> MONSTERS = new HashMap<>();
    public static final Random RANDOM = new Random();
    @EventHandler
    public void onSpawn(CreatureSpawnEvent e) {
        if (!ManhuntConfiguration.Fun.specialmobs) return;
        if (e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) return;
        if (e.getEntity().getType() == EntityType.ZOMBIE) {
            int rand = RANDOM.nextInt(11);
            if (rand < 1) {
                e.setCancelled(true);
                Zombie zombie = e.getEntity().getWorld().spawn(e.getLocation(), Zombie.class);
                EntityEquipment equipment = zombie.getEquipment();
                //noinspection ConstantConditions
                equipment.setHelmet(new ItemStack(Material.TNT));
                equipment.setHelmetDropChance(0.0F);
                zombie.setCustomName("§cBoomer Zombie");
                zombie.setCustomNameVisible(true);
                MONSTERS.put(zombie.getUniqueId(), SpecialMonster.BOOMER_ZOMBIE);
            }
        }
    }

    @EventHandler
    public void onEntityDeath(EntityDeathEvent e) {
        SpecialMonster type = MONSTERS.remove(e.getEntity().getUniqueId());
        if (type == SpecialMonster.BOOMER_ZOMBIE) {
            TNTPrimed tnt = e.getEntity().getWorld().spawn(e.getEntity().getLocation(), TNTPrimed.class);
            tnt.setCustomName("§cBoomer Zombie");
            MONSTERS.put(tnt.getUniqueId(), SpecialMonster.BOOMER_ZOMBIE_TNT);
        }
    }

    @EventHandler
    public void onBoomer(EntityExplodeEvent e) {
        SpecialMonster type = MONSTERS.remove(e.getEntity().getUniqueId());
        if (type == SpecialMonster.BOOMER_ZOMBIE_TNT) {
            Manhunt.runTask(() -> e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), new ItemStack(Material.TNT)));
        }
    }

    public enum SpecialMonster {
        BOOMER_ZOMBIE,
        BOOMER_ZOMBIE_TNT
    }

    public static void clearMonsters() {
        MONSTERS.keySet().forEach(m -> {
            Entity entity = Bukkit.getEntity(m);
            if (entity != null) {
                entity.remove();
            }
        });
        MONSTERS.clear();
    }
}
